import { observable, action, toJS, computed, autorun } from 'mobx';
import BaseWithList from 'stores/models/core/list';
import { Pages } from 'stores/models/core/nodeWithList';
import { findRootPages } from 'common/utils/store';

/**
 * Project
 */
export default class Project extends BaseWithList {
  icon = 'icon-page-toggle';
  label = '项目';
  type = 'project';

  @observable frontPages = null;
  @observable backPages = null;
  @observable mainPages = null;
  @observable motionVisible = false;
  @observable previewModalVisible = false;

  // 当前需要在模拟器内渲染的的页面列表对象
  @observable currentPages = null;

  @observable modals = [];

  constructor(...args) {
    super(...args);

    const mainPages = new Pages({parent: this, label: '主页面', type: 'mainPages'});
    const frontPages = new Pages({parent: this, label: '前景页', type: 'frontPages'});
    const backPages = new Pages({parent: this, label: '背景页', type: 'backPages'});

    let ajaxData = null;
    // todo request ajax data
    if (ajaxData) {
      // todo 灌装数据
      // mainPages.init({});
      // frontPages.init({});
      // backPages.init({});
    } else {
      mainPages.addPage();
      frontPages.addPage();
      backPages.addPage();
    }


    this.init({
      observableData: {
        list: [
          frontPages,
          backPages,
          mainPages,
        ],
      }
    });


    // 默认激活主页面
    this.setCurrentPages(mainPages);
  }

  @action setCurrentPages(currentPages, current) {
    const rootPages = findRootPages(currentPages);
    this.select(rootPages); // 保持 this.current 指向 this.list 子元素 的 Pages 对象，避免指向 slider 等继承Pages的对象

    this.currentPages = currentPages;
    // console.log(currentPages);
    if (current) {
      current.selectSelf();
    } else {
      currentPages.list[0].selectSelf();
    }
  }


}
